package com.example.zhaohang.selfeapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.zhaohang.utils.FragmentUtils;

import java.util.ArrayList;
import java.util.List;

public class ShowActivity extends BasicActivity {
    private BottomNavigationBar mNavigationBar;
    private List<Fragment> mList     = new ArrayList<>();
    //装任务栈
    private List<Fragment> mTask     = new ArrayList<>();
    //装底部的button
    private List<Integer>  mIntegers = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        for (int i = 0; i < 3; i++) {
            mList.add(FragmentUtils.provideFrage(i));
        }

        mNavigationBar = (BottomNavigationBar) findViewById(R.id.buttioncheged);
        mNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.avatar_9, "第一个").setActiveColorResource(R.color.colorAccent))
                .addItem(new BottomNavigationItem(R.mipmap.avatar3, "第二个").setActiveColorResource(R.color.colorAccent))
                .addItem(new BottomNavigationItem(R.mipmap.avatar5, "第三个").setActiveColorResource(R.color.colorAccent))//依次添加item,分别icon和名称
                .initialise();
        mNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                FragmentManager supportFragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                Fragment fragment = mList.get(position);
                for (Fragment fragment1 : mList) {
                    transaction.hide(fragment1);
                }

                if (!fragment.isAdded()) {
                    transaction.add(R.id.framelayout, fragment);
                }
                //压栈
                transaction.addToBackStack(null);
                transaction.show(fragment);
                transaction.commit();


                mTask.add(fragment);
                mIntegers.add(position);


            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

        initFirst();
    }

    private void initFirst() {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        Fragment fragment = FragmentUtils.provideFrage(0);
        fragmentTransaction.add(R.id.framelayout, fragment);
        fragmentTransaction.show(fragment);
        fragmentTransaction.commit();
        mTask.add(fragment);

    }

    @Override
    public void onBackPressed() {


        if (mTask.size() != 0) {
            Fragment fragment = mTask.get(mTask.size() - 2);
            int postion = mIntegers.get(mTask.size() - 2);
            //            mNavigationBar.selectTab(postion-1);
            FragmentManager supportFragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = supportFragmentManager.beginTransaction();

            for (Fragment fragment1 : mList) {
                transaction.hide(fragment1);
            }

            if (!fragment.isAdded()) {
                transaction.add(R.id.framelayout, fragment);
            }
            transaction.show(fragment);
            transaction.commit();
            mTask.remove(mTask.size() - 2);
        } else {
            super.onBackPressed();
        }
    }
}
